﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace WmsService.Util
{
    public class OutInStoreBillTypeFactory
    {
        /// <summary>
        /// 非正常出库对应入库代码的配置项key名称
        /// </summary>
        private const string AbnormalOutStoreBillCodeRelation = "AbnormalOutStoreBillCodeRelation";
        public static List<OutInStoreBillType> GetOutInStoreBillTypes()
        {
            string abnormalOutStoreBillCodeRelation = ConfigurationManager.AppSettings[AbnormalOutStoreBillCodeRelation];
            if(String.IsNullOrEmpty(abnormalOutStoreBillCodeRelation))
            {
                Exceptions.ExceptionHandler.ThrowException("请配置节点：" + AbnormalOutStoreBillCodeRelation);
            }
            string[] parts = abnormalOutStoreBillCodeRelation.Split(';');
            return (from part in parts
                    where part.Contains("|")
                    select part.Split('|')
                    into codes select new OutInStoreBillType(codes[0], codes[1])).ToList();
        }

        public static Customer GetAbnormalInStoreBillCustomer()
        {
            var ctx = Factory.ContextFactory.Create();
            var q = from c in ctx.Customer
                    where c.OWNERNO == "000000"
                    select c;
            var customer = q.FirstOrDefault();
            if(customer == null)
            {
                customer = Customer.CreateCustomer("000000");
                customer.OWNERNAME = "外借/维修专用名称";
                customer.OWNERSHORTCUT = "外借/维修";
                ctx.AddToCustomer(customer);
                ctx.ObjectStateManager.ChangeObjectState(customer, EntityState.Added);
                ctx.SaveChanges();
            }
            return customer;
        }
    }

    [DataContract]
    public class  OutInStoreBillType
    {
        public OutInStoreBillType(string outStoreCode,string relateInStoreCode)
        {
            this.OutStoreCode = outStoreCode;
            this.RelateInStoreCode = relateInStoreCode;
        }
        /// <summary>
        /// 出库类型代码
        /// </summary>
        [DataMember]
        public string OutStoreCode{get;  set;}

        /// <summary>
        /// 关联的入库类型代码
        /// </summary>
        [DataMember]
        public string RelateInStoreCode { get; set; }
    }
}